package com.sec.etech.ism.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sec.etech.ism.model.IsmAppSetting;
import com.sec.etech.ism.model.IsmServiceDef;
import com.sec.etech.ism.service.IsmAppSettingService;
import com.sec.etech.ism.service.IsmCommonService;
import com.sec.etech.ism.service.IsmMethodService;
import com.sec.etech.ism.service.IsmServiceDefService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class IsmSendPCSComponentBomServiceImpl implements IsmMethodService {
    @Resource
    IsmCommonService ismCommonService;
    @Resource
    IsmAppSettingService ismAppSettingService;
    @Resource
    IsmServiceDefService ismServiceDefService;

    @Override
    public JSONObject call(String sysCode, String serviceCode, JSONObject busObj) throws Exception {
        JSONObject result = new JSONObject();
        String msg = this.sendPCSComponentBom(sysCode, serviceCode, busObj);
        result.put("flag", msg);
        result.put("msg", msg);
        return result;
    }

    private String sendPCSComponentBom(String sysCode, String serviceCode, JSONObject busObj){
        Map<String, String> logMap = new HashMap<>(5);
        //服务信息
        IsmServiceDef serviceDef = null;
        try{
            serviceDef = ismServiceDefService.getByCode(serviceCode);
            //调用的应用信息
            IsmAppSetting appSetting = ismAppSettingService.getByCode(sysCode);
            String url = appSetting.getAppAddress() + "prodInfo/component";
            logMap.put("appName", this.methodName());
            logMap.put("url", url);
            JSONObject pcsParams = ismCommonService.sendComponentBom(busObj.getLong("componentid"));
            ismCommonService.checkResult(url, pcsParams.toJSONString(), serviceDef, null, busObj.toJSONString(), this.methodName(), logMap);
            return "success";
        }catch (Exception e){
            log.error("部套BOM信息推送PCS接口异常。serviceDef.getCode()={}，serviceDef.getName()={}，logMap={}",
                    serviceDef.getCode(), serviceDef.getName(), logMap);
            e.printStackTrace();
            return "部套BOM信息推送PCS接口异常，请联系管理员处理！";
        }
    }

    @Override
    public String methodName() {
        return "sendPCSComponentBom";
    }
}
